package me.huha.client.utils;

import java.security.GeneralSecurityException;
import java.util.Properties;

import com.sun.mail.util.MailSSLSocketFactory;

import me.huha.commons.email.EmailSetDTO;
import me.huha.commons.email.PropertiesDTO;
import me.huha.commons.email.ProtocolEnum;

/**
 * @Title: PropertiesUtils.java
 * @Author: Yepx
 * @mail: yepx2013@gmail.com
 * @CreateTime: 2017年9月29日 上午11:26:06
 * @Todo: 获取配置文件类
 */
public class PropertiesUtils {
	
	/** 发送协议 */
	private EmailSetDTO emailSetSend;
	/** 接收协议 */
	private EmailSetDTO emailSetGet;
	
	private Long timeout = 5000L;
	
	private Long connectiontimeout = 5000L;
	
	public PropertiesUtils(EmailSetDTO emailSetSend, EmailSetDTO emailSetGet) {
		super();
		this.emailSetSend = emailSetSend;
		this.emailSetGet = emailSetGet;
	}
	
	/**
	 * 获得配置信息
	 * @return
	 */
	public PropertiesDTO getProperties(){
		
		ProtocolEnum sendEnum = ProtocolEnum.getProtocolEnum(emailSetSend.getTransport());
		ProtocolEnum getEnum = ProtocolEnum.getProtocolEnum(emailSetGet.getTransport());
		
		Properties props = new Properties();
		/** send 配置 */
		props.put("mail.smtp.host", emailSetSend.getHost());
	    props.put("mail.smtp.port", emailSetSend.getPort());
	    props.put("mail.smtp.auth", true);
	    props.put("mail.smtp.ssl.enable", emailSetSend.getSslEnable());
	    props.put("mail.smtp.timeout", timeout);
        props.put("mail.smtp.connectiontimeout", connectiontimeout);
	    //SSL 认证
	    if(emailSetSend.getSslEnable()){
        	MailSSLSocketFactory sf = null;
            try {
                sf = new MailSSLSocketFactory();
            } catch (GeneralSecurityException e) {
               e.getStackTrace();
            }
            sf.setTrustAllHosts(true);
            props.put("mail.smtp.ssl.socketFactory", sf);
	    }
		/** get 配置 */
		if(getEnum.equals(ProtocolEnum.IMAP)){
			
		    props.put("mail.imap.port", emailSetGet.getPort());              // 端口  
		    props.put("mail.imap.host", emailSetGet.getHost());    		   // pop3服务器  
		    props.put("mail.imap.timeout", timeout);
		    props.put("mail.imap.connectiontimeout", connectiontimeout);
		    props.put("mail.imap.ssl.enable", emailSetGet.getSslEnable());
	        //SSL 认证
	        if(emailSetGet.getSslEnable()){
	        	MailSSLSocketFactory sf = null;
	            try {
	                sf = new MailSSLSocketFactory();
	            } catch (GeneralSecurityException e) {
	               e.getStackTrace();
	            }
	            sf.setTrustAllHosts(true);
	            props.put("mail.imap.ssl.socketFactory", sf);
	        }
	        
		}else{
			
			props.put("mail.pop3.port", emailSetGet.getPort());              // 端口  
		    props.put("mail.pop3.host", emailSetGet.getHost());    		   // pop3服务器  
		    props.put("mail.pop3.timeout", timeout);
		    props.put("mail.pop3.connectiontimeout", connectiontimeout);
		    props.put("mail.pop3.ssl.enable", emailSetGet.getSslEnable());
	        //SSL 认证
	        if(emailSetGet.getSslEnable()){
	        	MailSSLSocketFactory sf = null;
	            try {
	                sf = new MailSSLSocketFactory();
	            } catch (GeneralSecurityException e) {
	               e.getStackTrace();
	            }
	            sf.setTrustAllHosts(true);
	            props.put("mail.pop3.ssl.socketFactory", sf);
	        }
		}
		PropertiesDTO propertiesDto = new PropertiesDTO(props, sendEnum, getEnum);
		
		return propertiesDto;
	}
}
